﻿using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using CEMS_DHKH.Models.Mapping;
using System;

namespace CEMS_DHKH.Models
{
    public partial class CEMS_DHKhoaHocContext : DbContext
    {
        static CEMS_DHKhoaHocContext()
        {
            Database.SetInitializer<CEMS_DHKhoaHocContext>(null);
        }

        public CEMS_DHKhoaHocContext()
            : base("Name=CEMS_DHKhoaHocContext")
        {
        }

        public CEMS_DHKhoaHocContext(System.Data.Common.DbConnection existingConnection, bool contextOwnsConnection = false)
            : base(existingConnection, contextOwnsConnection)
        {
        }

        public static CEMS_DHKhoaHocContext GetContext(System.Data.Common.DbConnection existingConnection, bool contextOwnsConnection = false)
        {
            return new CEMS_DHKhoaHocContext(existingConnection, contextOwnsConnection);
        }

        public static System.Data.Common.DbConnection GetDbConnection()
        {
            System.Data.Common.DbConnection dbConnection = null;
            try
            {
                var dbProviderFactory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.Sqlclient");
                dbConnection = dbProviderFactory.CreateConnection();
                dbConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CEMS_DHKhoaHocContext"].ConnectionString;
                return dbConnection;
            }
            catch (Exception exc)
            {
                throw new Exception("Không kết nối đến CSDL đào tạo Đại học được", exc);
            }
        }

        public static CEMS_DHKhoaHocContext GetContext()
        {
            //var defaultContext = new CEMS_DHKhoaHocContext();
            //return defaultContext;

            System.Diagnostics.Debug.WriteLine("Bắt đầu tạo DbContext DH" + System.DateTime.Now.ToString("hh:MM:ss"));
            System.Data.Common.DbConnection dbConnection = null;
            try
            {
                dbConnection = GetDbConnection();
                dbConnection.Open();
                System.Diagnostics.Debug.WriteLine("Đã tạo xong dbConnection DHKH" + System.DateTime.Now.ToString("hh:MM:ss"));
                var context = new CEMS_DHKhoaHocContext(dbConnection, true);
                System.Diagnostics.Debug.WriteLine("Đã tạo xong DbContext DHKH" + System.DateTime.Now.ToString("hh:MM:ss"));
                return context;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi tạo DbContext DHKH" + ex);
            }
        }

        public DbSet<GiangVienDaoTaoDaiHoc> GiangVienDaoTaoDaiHocDbSet { get; set; }
        public DbSet<ThoiKhoaBieuDaoTaoDaiHoc> ThoiKhoaBieuDaoTaoDaiHocDbSet { get; set; }
        public DbSet<DonViDaoTaoDaiHoc> DonViDaoTaoDaiHocDbSet { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {            
            modelBuilder.Configurations.Add(new GiangVienDaoTaoDaiHocMap());
            modelBuilder.Configurations.Add(new ThoiKhoaBieuDaoTaoDaiHocMap());
            modelBuilder.Configurations.Add(new DonViDaoTaoDaiHocMap());
        }
    }
}
